{extend name="layout/default" /}
{block name="head"}
<title>菜单列表</title>
{/block}
{block name="content"}
<div class="ok-body">
	<!--模糊搜索区域-->
	<div class="layui-row">

	</div>
	<!--数据表格-->
	<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
{/block}
{block name="scripts"}
<!--js逻辑-->
<!--<script src="/static/lib/layui/lay/lay-config.js"></script>-->
<script>
	layui.use(["jquery", "table", 'treetable', "form"], function () {
		let table = layui.table;
		let treetable = layui.treetable;
		let form = layui.form;

		let $ = layui.jquery;

		let userTable = treetable.render({
			treeColIndex: 2,
			treeSpid: 0,
			treeDefaultClose: true,
			treeIdName: 'id',
			treePidName: 'pid',
			elem: '#tableId',
			url: '{:url("index")}',
			//limit: 20,
			page: false,
			toolbar: true,
			toolbar: "#toolbarTpl",
			//height:'full',
			cols: [[
				{ type: "checkbox", width: 50 },
				{ field: 'id', width: 80, title: 'ID', sort: true, align: 'center', },
				{ field: 'title', minWidth: 250, title: '菜单名' },
				{ field: 'name', title: '链接' },
				{ field: 'weigh', title: '权重' },
				{ field: 'icon', title: '图标', templet: "#iconTpl" },
				{ field: "ismenu", title: "是否菜单", width: 100, templet: "#ismenuTpl" },
				{ field: "status", title: "状态", width: 100, templet: "#statusTpl" },
				//{field: 'createtime_text',  title: '创建时间'},
				{ title: "操作", align: "center", fixed: "right", templet: "#operationTpl" }
			]],
			done: function (res, curr, count) {
				console.info(res, curr, count);
			}
		});
		function expand() {
			treetable.expandAll('#tableId');
		}

		function fold() {
			treetable.foldAll('#tableId');
		}
		form.on("submit(search)", function (data) {
			userTable.reload({
				where: data.field,
				page: { curr: 1 }
			});
			return false;
		});

		table.on("toolbar(tableFilter)", function (obj) {
			switch (obj.event) {
				case "batchEnabled":
					batchEnabled();
					break;
				case "batchDisabled":
					batchDisabled();
					break;
				case "batchDel":
					batchDel();
					break;
				case "add":
					add();
					break;
				case "expand":
					expand();
					break;
				case "fold":
					fold();
					break;
			}
		});

		table.on("tool(tableFilter)", function (obj) {
			let data = obj.data;
			switch (obj.event) {
				case "edit":
					edit(data);
					break;
				case "del":
					del(data.id);
					break;
			}
		});

		function batchEnabled() {
		    let data = table.checkStatus('tableId').data;
                    if (data.length === 0) {
                        layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    var idsStr = ""
                    var hasCheck = table.checkStatus('tableId')
                    var hasCheckData = hasCheck.data
                    if (hasCheckData.length > 0) {
                        $.each(hasCheckData, function (index, element) {
                            //idsStr.push(element.id)
                            idsStr += element.id + ",";
                        })
                    }
			layer.confirm("确定要批量启用吗？", function (index) {
				layer.close(index);
				if (idsStr) {
					$.ajax(
					    {
					    url:"{:url('setNormal')}", 
					    type:"put", 
					    data:{ idsStr: idsStr },
					    }).done(function (response) {
						if (response.code == 1) {
							console.log(response);
						layer.msg(response.msg,{icon:1,time:1000},function(){
							location.reload();})
						} else {
						layer.msg(response.msg,{icon:2,time:1000},function(){

							})
						}
					}).fail(function (error) {
						console.log(error)
					});
				}
			});
		}

		function batchDisabled() {
		    	    let data = table.checkStatus('tableId').data;
                    if (data.length === 0) {
                        layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    var idsStr = ""
                    var hasCheck = table.checkStatus('tableId')
                    var hasCheckData = hasCheck.data
                    if (hasCheckData.length > 0) {
                        $.each(hasCheckData, function (index, element) {
                            //idsStr.push(element.id)
                            idsStr += element.id + ",";
                        })
                    }
			layer.confirm("确定要批量停用吗？", function (index) {
				layer.close(index);
				if (idsStr) {
					$.ajax(
					    {
					        url:"{:url('setStop')}", 
					        type:"put", 
					        data:{ idsStr: idsStr },
					        }).done(function (response) {

						if (response.code == 1) {
							console.log(response);
							 layer.msg(response.msg,{icon:1,time:1000},function(){
							location.reload();});
						} else {
							 layer.msg(response.msg,{icon:2,time:1000},function(){

							})
						}
					}).fail(function (error) {
						console.log(error)
					});
				}
			});
		}
		
		
		/**
       * 主要用于针对表格批量操作操作之前的检查
       * @param table
       * @returns {string}
       */
    //   tableBatchCheck: function (table) {
    //      var checkStatus = table.checkStatus("tableId");
    //      var rows = checkStatus.data.length;
    //      if (rows > 0) {
    //         var idsStr = "";
    //         for (var i = 0; i < checkStatus.data.length; i++) {
    //           idsStr += checkStatus.data[i].id + ",";
    //         }
    //         return idsStr;
    //      } else {
    //         layer.msg("未选择有效数据", {offset: "t", anim: 6});
    //      }
    //   },

		function batchDel() {
		    let data = table.checkStatus('tableId').data;
                    if (data.length === 0) {
                        layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    var idsStr = ""
                    var hasCheck = table.checkStatus('tableId')
                    var hasCheckData = hasCheck.data
                    if (hasCheckData.length > 0) {
                        $.each(hasCheckData, function (index, element) {
                            //idsStr.push(element.id)
                            idsStr += element.id + ",";
                        })
                    }
			layer.confirm("确定要批量删除吗？", function (index) {
				layer.close(index);
				if (idsStr) {
					$.ajax({

					        url:'delete',
                            data:{idsStr: idsStr},
                            dataType: 'json',
                            type: 'delete',

			           	}).done(function (response) {
						//console.log(response);
						//okUtils.tableSuccessMsg(response.msg);
						if (response.code == 1) {
							console.log(response);
									layer.msg(response.msg, {
                                        icon: 1,
                                        time: 1000
                                    }, function() {
                                    	//table.reload('tableId');//奇怪用这个表格不刷新
                                    	location.reload();
                                    });
							//location.reload();
						} else {
								layer.msg(response.msg, {
                                        icon: 2,
                                        time: 1000
                                    },  function () {

							})
						}
					}).fail(function (error) {
						console.log(error)
					});
				}
			});
		}

		function add() {
			layer.open(
			    
			    
			      {
			        
			            type: 2,
                        maxmin: true,
                        title: '添加',
                        shade: 0.1,
                        area: ['90%', '90%'],
                        content: '{:url('add')}'
			        
			    },
			    
			    
			    function () {
				location.reload();
			})
		}

		function edit(data) {
			layer.open(
			{
                        type: 2,
                        maxmin: true,
                        title: '{:__('Edit')}',
                        shade: 0.1,
                        area: ['90%', '90%'],
						content: 'edit?id='+ data.id
					}, function (layero) {
				//let iframeWin = window[layero.find("iframe")[0]["name"]];
				//iframeWin.initForm(data);
			}, function () {
				location.reload();
			})
		}

		function del(id) {
			layer.confirm("确定要删除吗？", function () {
				$.ajax(
				    {
				        url:"{:url('delete')}",
				        type:"delete",
				        data:{ idsStr: id },
				        dataType:"json",
				        
				    }).done(function (response) {


					if (response.code == 1) {
						console.log(response);
						layer.msg(response.msg,{icon:1,time:1000},function(){
						location.reload();});
					} else {
						layer.msg(response.msg,{icon:2,time:1000},function(){

						})
					}

				}).fail(function (error) {
					console.log(error)
				});
			})
		}
	})
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
		<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="batchEnabled">批量启用</button>
		<button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="batchDisabled">批量停用</button>
		<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDel">批量删除</button>
	
		 <button class="layui-btn" id="btn-expand" lay-event="expand">全部展开</button>
		  <button class="layui-btn layui-btn-normal" id="btn-fold" lay-event="fold">全部折叠</button>
	</div>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
		<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="del"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="statusTpl">
	{{#  if(d.status == 'normal'){ }}
	<span class="layui-btn layui-btn-normal layui-btn-xs">启用</span>
	{{#  } else if(d.status != 'normal') { }}
	<span class="layui-btn layui-btn-warm layui-btn-xs">停用</span>
	{{#  } }}
</script>
<script type="text/html" id="iconTpl">
	<i class="fa {{d.icon}}"></i>
</script>
<script type="text/html" id="ismenuTpl">
	{{#  if(d.ismenu == 0){ }}
	<span class="layui-btn layui-btn-warm layui-btn-xs">否</span>
	{{#  } else if(d.ismenu != 0) { }}
	<span class="layui-btn layui-btn-normal layui-btn-xs">是</span>
	{{#  } }}
</script>
{/block}